<?php
session_start();
include ('mysqli_connect.php');

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
    
    
    <link rel="stylesheet" href="../../bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" href="../../bootstrap/awesome-bootstrap-checkbox/bower_components/Font-Awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="../../bootstrap/awesome-bootstrap-checkbox/demo/build.css"/>

    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <title>影视管理 || 用户注册</title>
</head>
<body>

<nav class="navbar navbar-default navbar-static-top" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">影视管理系统</a>
        </div>
        <div>
            <ul class="nav navbar-nav">
                <li class="active"><a href="admin_index.php">主页</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">影视管理<b class="caret"></b>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="admin_all_movie.php">全部影视</a></li>
                        <li><a href="admin_movie_add.php">增加影视信息</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">用户管理<b class="caret"></b>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="admin_user.php">全部用户</a></li>
                        <li><a href="admin_user_add.php">增加用户</a></li>
                        <?php 
                            if($_SESSION['super_manager']==2){
                                echo "<li><a href='super_admin_add.php'>增加管理员</a></li>";
                            }
                        ?>
                    </ul>
                </li>
                <li><a href="admin_repass.php">密码修改</a></li>
                <li><a href="index.php">退出</a></li>
            </ul>
        </div>
    </div>
</nav>

<?php
    $sql_temp = "select max(user_id) as user_id from user_table;";
    $res_temp = mysqli_query($dbc,$sql_temp);
    if($res_temp != false)
    {
        $nnid = mysqli_fetch_assoc($res_temp)['user_id'];
        $nnid = (int)$nnid;
        /*if(is_string($nnid))
        {
            echo "<script>alert('这是一个字符串！');</script>";
        }
        else{
            echo "<script>alert('这不是一个字符串！');</script>";
        }*/
        $nnid = $nnid + 1;
        $nnid = (string)$nnid;
    }
    
?>

<h1 style="text-align: center"><strong>增加用户</strong></h1>
<div style="padding: 10px 500px 10px;">
    <form  action="admin_user_add.php" method="POST" style="text-align: center" class="bs-example bs-example-form" role="form">
        <div id="login">
            <div class="input-group"><span class="input-group-addon">ID</span><input name="nnid" type="text" placeholder="ID不可重复！建议的ID ：<?php echo $nnid;?>" class="form-control"></div><br/>
            <div class="input-group"><span class="input-group-addon">昵称</span><input name="nname" type="text" placeholder="请输入昵称" class="form-control"></div><br/>
            <div class="input-group"><span class="input-group-addon">年龄</span><input name="nage" type="text" placeholder="请输入年龄" class="form-control"></div><br/>
            <div class="input-group"><span class="input-group-addon">密码</span><input name="npass" type="text" placeholder="请输入密码" class="form-control"></div><br/>
            
            <div class="radio">
                <span>性别</span>
                <label> <input name="nsex" type="radio" value="男" class="checkbox_circle">男 </label>
                <label> <input name="nsex" type="radio" value="女" class="checkbox_circle">女</label>
            </div><br/>
            <input type="submit" value="添加" class="btn btn-default">
            <input type="reset" value="重置" class="btn btn-default">
        </div>
    </form>
</div>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $nnid = $_POST["nnid"];
    $nnam = $_POST["nname"];
    $nage =$_POST["nage"];
    $npass= $_POST["npass"];
    $nsex = $_POST["nsex"];
    
    /*$nadd= $_POST["naddress"];
    $nnte = $_POST["ntel"];*/
    $sql_temp1 = "select * from user_table where user_name='{$nnam}';";
    $sql_temp2 = "select * from manager_table where manager_name='{$nnam}';";
    $res_temp1 = mysqli_query($dbc,$sql_temp1);
    $res_temp2 = mysqli_query($dbc,$sql_temp2);
    if(mysqli_num_rows($res_temp1) >= 1 || mysqli_num_rows($res_temp2) >= 1)
    {
        echo "<script>alert('用户名已经存在，请重新输入!')</script>";
    }
    else 
    {
        if($nnid == "")
        {
            $sql_temp = "select max(user_id) as user_id from user_table;";
            $res_temp = mysqli_query($dbc,$sql_temp);
            if($res_temp == 1)
            {
                $nnid = mysqli_fetch_assoc($res_temp)['user_id'];
                $nnid = (int)$nnid;
                /*if(is_string($nnid))
                {
                    echo "<script>alert('这是一个字符串！');</script>";
                }
                else{
                    echo "<script>alert('这不是一个字符串！');</script>";
                }*/
                $nnid = $nnid + 1;
                $nnid = (string)$nnid;
            }
    
            $sqla="INSERT INTO `user_table`
                (`user_id`,
                `user_age`,
                `user_sex`,
                `user_name`,
                `user_password`)
                VALUES
                ('{$nnid}',
                $nage,
                '{$nsex}',
                '{$nnam}',
                '{$npass}');";
    
            //$sqlb="insert into reader_card (reader_id,name) VALUES($nnid ,'{$nnam}');";
            $resa=mysqli_query($dbc,$sqla);
            //$resb=mysqli_query($dbc,$sqlb);
    
    
            if($resa==1)
            {
                echo "<script>alert('用户添加成功！')</script>";
                echo "<script>window.location.href='admin_user_add.php'</script>";
    
            }
            else
            {
                echo "<script>alert('添加失败！请重新输入！');</script>";
    
            }
        }
        else
        {
            $sql_temp = "select count(*) as count_num from user_table where user_id = '{$nnid}';";
            $res_temp = mysqli_query($dbc,$sql_temp);
            if($res_temp !=false )
            {
                if(mysqli_fetch_assoc($res_temp)['count_num'] == '0')
                {
                    $sqla="INSERT INTO `user_table`
                        (`user_id`,
                        `user_age`,
                        `user_sex`,
                        `user_name`,
                        `user_password`)
                        VALUES
                        ('{$nnid}',
                        $nage,
                        '{$nsex}',
                        '{$nnam}',
                        '{$npass}');";
                    $resa = mysqli_query($dbc,$sqla);
                    if($resa != false)
                    {
                        echo "<script>alert('用户添加成功！')</script>";
                        echo "<script>window.location.href='admin_user_add.php'</script>";
                    }
                    else 
                    {
                        echo "<script>alert('添加失败！请重新输入！');</script>";
                    }
                }
                else 
                {
                    echo "<script>alert('不能添加重复的ID！请重新输入！');</script>";
                }
            }
        }
    }
    
    
}


?>
</body>
</html>
